﻿

Public Class NetworkPaket
    Public Const MaxRecieveBuffer As Integer = 1048576
    Public Bytes As Byte()
    Public Position As Integer
    Public Sub New()
    End Sub
    Public Sub New(ByVal Bytes As Byte())
        Me.Bytes = Bytes
        Me.Position = 0
    End Sub
    Public Sub New(ByVal Data As String)
        Me.Bytes = Hauptmenue.Codierung.GetBytes(Data)
        Me.Position = 0
    End Sub
    Public Function Read(ByVal Length As Integer) As Byte()
        If Position + Length < Bytes.Length Then
            Dim B(Length) As Byte
            For I = Position To Position + Length
                B(I - Position) = Bytes(I)
            Next
            Position += Length
            Return B
        Else
            Dim B(Bytes.Length - Position - 1) As Byte
            For I = Position To Bytes.Length - 1
                B(I - Position) = Bytes(I)
            Next
            Position = Bytes.Length - 1
            Return B
        End If
    End Function
    Public Function ReadAll() As Byte()
        Dim SourceByte() As Byte = {}
        ReDim SourceByte(Bytes.Length - Position - 1)
        Array.ConstrainedCopy(Bytes, Position, SourceByte, 0, Bytes.Length - Position)
        Return SourceByte
    End Function
    Public Function Read() As Byte
        If Position < Bytes.Length Then
            Dim B As Byte = Bytes(Position)
            Position += 1
            Return B
        Else
            Return 0
        End If
    End Function
    Public Function ReadString() As String
        Return Hauptmenue.Codierung.GetString(Bytes)
    End Function
    Public Sub Restart()
        Position = 0
    End Sub
    Public Sub Write(ByVal Data As String)

        Dim NBytes As Byte() = Hauptmenue.Codierung.GetBytes(Data)
        Dim StartPos As Integer = 0
        If Bytes IsNot Nothing Then
            StartPos = Bytes.Length
        End If
        Array.Resize(Bytes, StartPos + NBytes.Length)
        For I = StartPos To Bytes.Length - 1
            Bytes(I) = NBytes(I - StartPos)
        Next
    End Sub
    Public Sub Write(ByVal NBytes As Byte())
        Dim StartPos As Integer = 0
        If Bytes IsNot Nothing Then
            StartPos = Bytes.Length
        End If
        Array.Resize(Bytes, StartPos + NBytes.Length)
        For I = StartPos To Bytes.Length - 1
            Bytes(I) = NBytes(I - StartPos)
        Next
    End Sub
    Public Function Length() As Integer
        Return Bytes.Length
    End Function
    Public Shared Function GenerateByteStream(ByVal sendBytes As Byte()) As Byte()
        Dim StartBytes As Byte() = Hauptmenue.Codierung.GetBytes(Net_Orders.Start0000000000000000_.ToString)
        Dim EndBytes As Byte() = Hauptmenue.Codierung.GetBytes(Net_Orders._End0000000000000000.ToString)
        Dim B(StartBytes.Length + sendBytes.Length + EndBytes.Length - 1) As Byte
        For I = 0 To StartBytes.Length - 1
            B(I) = StartBytes(I)
        Next
        For I = StartBytes.Length To StartBytes.Length + sendBytes.Length - 1
            B(I) = sendBytes(I - StartBytes.Length)
        Next
        For I = StartBytes.Length + sendBytes.Length To B.Length - 1
            B(I) = EndBytes(I - StartBytes.Length - sendBytes.Length)
        Next
        Return B
    End Function
    Public Shared Function GenerateStringStream(ByVal Data As String) As String
        Data = Net_Orders.Start0000000000000000_.ToString + Data + Net_Orders._End0000000000000000.ToString
        Return Data
    End Function

End Class
Public Class ServerPaket
    Inherits NetworkPaket
    Public PlayerID As Integer
    Public All_Player As Boolean
    Public Only_To_Player As Boolean
    Public Sub New(ByVal Bytes As Byte(), ByVal PlayerID As Integer, Optional ByVal All_Player As Boolean = False, Optional ByVal Only_To_Player As Boolean = False)
        MyBase.New(Bytes)
        Me.PlayerID = PlayerID
        Me.Only_To_Player = Only_To_Player
        Me.All_Player = All_Player
        If Me.All_Player And Me.Only_To_Player Then
            Me.All_Player = False
        End If
    End Sub
    Public Sub New(ByVal Data As String, ByVal PlayerID As Integer, Optional ByVal All_Player As Boolean = False, Optional ByVal Only_To_Player As Boolean = False)
        MyBase.New(Data)
        Me.PlayerID = PlayerID
        Me.Only_To_Player = Only_To_Player
        Me.All_Player = All_Player
        If Me.All_Player And Me.Only_To_Player Then
            Me.All_Player = False
        End If
    End Sub
End Class
Public Enum Net_Orders
    ''' <summary>
    ''' A Player is ready for a Game
    ''' </summary>
    ''' <remarks></remarks>
    Player_Ready
    ''' <summary>
    ''' A Player isn't ready for a Game.
    ''' </summary>
    ''' <remarks></remarks>
    Player_Not_Ready
    ''' <summary>
    ''' The Game is loaded by this Player
    ''' </summary>
    ''' <remarks></remarks>
    Game_Loaded
    ''' <summary>
    ''' Paket Start
    ''' </summary>
    ''' <remarks></remarks>
    Start0000000000000000_
    ''' <summary>
    ''' Paket_End
    ''' </summary>
    ''' <remarks></remarks>
    _End0000000000000000
    ''' <summary>
    ''' A Client wants to disconnect
    ''' </summary>
    ''' <remarks></remarks>
    Close_Connection
    ''' <summary>
    ''' Der Host oder Server hat die Nachricht erhalten
    ''' </summary>
    ''' <remarks></remarks>
    MessageRecieved
End Enum